package com.jumi.microservice.service.taobao;

import com.alibaba.fastjson.JSON;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.SendResult;
import com.aliyun.openservices.ons.api.bean.ProducerBean;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jumi.microservice.dto.CouponOrderMessage;
import com.jumi.microservice.entity.CouponOrder;
import com.jumi.microservice.mapper.CouponOrderMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import java.util.List;

/**
 * @author Dirk
 * @Date 2021-01-14 16:13
 */
public class TaoBaoOrderHandler implements Runnable {

    private static final Logger log = LoggerFactory.getLogger(TaoBaoOrderHandler.class);

    private List<CouponOrder> couponOrderList;

    private CouponOrderMapper couponOrderMapper;

    private ProducerBean producerBean;

    public TaoBaoOrderHandler(List<CouponOrder> couponOrderList, CouponOrderMapper couponOrderMapper, ProducerBean producerBean) {
        this.couponOrderList = couponOrderList;
        this.couponOrderMapper = couponOrderMapper;
        this.producerBean = producerBean;
    }

    private void saveList(List<CouponOrder> couponOrderList) {
        couponOrderList.forEach(couponOrder -> {
            QueryWrapper<CouponOrder> couponOrderQw = new QueryWrapper<>();
            couponOrderQw.eq("order_sn", couponOrder.getOrderSn());
            CouponOrder targetCouponOrder = couponOrderMapper.selectOne(couponOrderQw);
            if (targetCouponOrder == null) {
                log.info("异步处理淘宝订单--> 插入订单: [{}], 订单信息: [{}]", couponOrder.getOrderSn(), JSON.toJSONString(couponOrder));
                couponOrderMapper.insert(couponOrder);

                CouponOrderMessage couponOrderMessage = new CouponOrderMessage();
                BeanUtils.copyProperties(couponOrder, couponOrderMessage);
                Message message = new Message("couponOrder", "", "", JSON.toJSONBytes(couponOrderMessage));
                SendResult sendResult = producerBean.send(message);
                log.info("淘宝订单[{}]发送消息, 消息ID[{}]", couponOrder.getOrderSn(), sendResult.getMessageId());
            } else {
                if (targetCouponOrder.getCouponOrderStatus().equals(couponOrder.getCouponOrderStatus())) {
                    log.info("异步处理淘宝订单--> 忽略订单: [{}], 订单信息: [{}]", couponOrder.getId(), JSON.toJSONString(couponOrder));
                    return;
                }
                couponOrder.setId(targetCouponOrder.getId());
                couponOrder.setCreateTime(targetCouponOrder.getCreateTime());
                log.info("异步处理淘宝订单--> 更新订单: [{}], 订单信息: [{}]", couponOrder.getId(), JSON.toJSONString(couponOrder));
                couponOrderMapper.updateById(couponOrder);

                CouponOrderMessage couponOrderMessage = new CouponOrderMessage();
                BeanUtils.copyProperties(couponOrder, couponOrderMessage);
                Message message = new Message("couponOrder", "", "", JSON.toJSONBytes(couponOrderMessage));
                SendResult sendResult = producerBean.send(message);
                log.info("淘宝订单[{}]发送消息, 消息ID[{}]", couponOrder.getOrderSn(), sendResult.getMessageId());
            }
            try {
                Thread.sleep(0);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }

    @Override
    public void run() {
        log.info("异步处理淘宝订单--> 开始处理订单, 订单总量[{}]", couponOrderList.size());
        long start = System.currentTimeMillis();
        saveList(couponOrderList);
        long end = System.currentTimeMillis();
        log.info("异步处理淘宝订单--> 订单处理完成, 耗时[{}]ms", end - start);

    }
}
